<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>
        <ui:composition template="../templates/default3.xhtml">
            <ui:define name="content">
                <h:form binding="#{creditManagerBean.loadOutstandingInvoices}">
                    <p:panel header="Invoices" id="invoicesPanel">
                        <p:messages />
                        
                        <p:dataTable id="invoices" var="invoice" value="#{creditManagerBean.outstandingInvoices}" 
                                     emptyMessage="No invoices found with given criteria."
                                     selection="#{creditManagerBean.selectedInvoice}"
                                     selectionMode="single"
                                     rowSelectListener="#{creditManagerBean.outstandingInvoicesOnRowSelect}"
                                     onRowSelectUpdate="purchaseDetailsPanel,customerDetailsPanel" 
                                     onRowUnselectUpdate="purchaseDetailsPanel,customerDetailsPanel"
                                     rowStyleClass="#{invoice.invoiceDate le creditManagerBean.todayTimestampMinus30 
                                                      and invoice.status eq 'Pending Payment' ? 'dRed' : 
                                                      ((invoice.invoiceDate ge creditManagerBean.todayTimestampMinus30) 
                                                      and (invoice.invoiceDate le creditManagerBean.todayTimestampMinus7)
                                                      and (invoice.status eq 'Pending Payment')) ? 'dOrange' : null }">
                            <p:column headerText="Invoice ID" sortBy="#{invoice.id}">  
                                <h:outputText value="#{invoice.id}">
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Invoice Date" 
                                      sortBy="#{invoice.invoiceDate}">  
                                <h:outputText value="#{invoice.invoiceDate}"> 
                                    <f:convertDateTime pattern="dd-MM-yyyy hh:mm a" timeZone="#{defaultManagerBean.tz}"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Invoice Amount" sortBy="#{invoice.amount}">  
                                <h:outputText value="#{invoice.amount}">  
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Type" sortBy="#{invoice.type}">  
                                <h:outputText value="#{invoice.type}"> 
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Status" sortBy="#{invoice.status}">  
                                <h:outputText value="#{invoice.status}">
                                </h:outputText>
                            </p:column> 
                        </p:dataTable>
                    </p:panel>
                    <p:panel header="Customer Details" id="customerDetailsPanel">
                        <table width="100%" border="0">
                            <tr>
                                <td width="200px">
                                    Company:
                                </td>
                                <td>
                                    <h:outputText id="company" value="#{creditManagerBean.selectedCustomer.company}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Description:
                                </td>
                                <td>
                                    <h:outputText id="description" value="#{creditManagerBean.selectedCustomer.description}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Annual Revenue:
                                </td>
                                <td>
                                    <h:outputText id="revenue" value="#{creditManagerBean.selectedCustomer.revenue}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Number of Employees:
                                </td>
                                <td>
                                    <h:outputText id="numEmployee" value="#{creditManagerBean.selectedCustomer.numEmployee}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Person
                                </td>
                                <td>
                                    <h:outputText id="contactPerson" value="#{creditManagerBean.selectedCustomer.firstName}, 
                                                  #{creditManagerBean.selectedCustomer.lastName}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Designation:
                                </td>
                                <td>
                                    <h:outputText id="title" value="#{creditManagerBean.selectedCustomer.title}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Number:
                                </td>
                                <td>
                                    <h:outputText id="phone" value="#{creditManagerBean.selectedCustomer.phone}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Credit Rank:
                                </td>
                                <td>
                                    <h:outputText id="rank" value="#{creditManagerBean.selectedCustomer.credit.creditRank}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Credit Limit:
                                </td>
                                <td>
                                    <h:outputText id="limit" value="#{creditManagerBean.selectedCustomer.credit.creditLimit}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:panel header="Purchase Details" id="purchaseDetailsPanel">
                        <table width="100%" border="0">
                            <tr>
                                <td width="200px">Product Name:</td>
                                <td>
                                    <h:outputText id="name" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.product.name}" 
                                                  title="Name of product.">
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">Quantity:</td>
                                <td>
                                    <h:outputText id="quantity" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.quantity}" 
                                                  title="Order quantity.">
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">Request Date:</td>
                                <td>
                                    <h:outputText id="requestDate" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.requestDate}" 
                                                  title="Date of request.">
                                        <f:convertDateTime pattern="dd-MM-yyyy" timeZone="#{defaultManagerBean.tz}"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">Shipping Address:</td>
                                <td>
                                    <h:outputText id="shippingAddress" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.shippingAddress.street}, 
                                                  #{creditManagerBean.selectedInvoice.purchaseItem.shippingAddress.city}, 
                                                  #{creditManagerBean.selectedInvoice.purchaseItem.shippingAddress.country}" 
                                                  title="Shipping address.">
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">Discount:</td>
                                <td>
                                    <h:outputText id="invoiceType" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.discount}" 
                                                  title="Discount given.">
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">Subtotal:</td>
                                <td>
                                    <h:outputText id="subtotal" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.subTotal}" 
                                                  title="Order subtotal.">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">Outstanding Amount:</td>
                                <td>
                                    <h:outputText id="outstandingAMount" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.outstandingAmount}" 
                                                  title="Outstanding amount.">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">Order Status:</td>
                                <td>
                                    <h:outputText id="orderStatus" 
                                                  value="#{creditManagerBean.selectedInvoice.purchaseItem.status}" 
                                                  title="Status of order.">
                                    </h:outputText>
                                </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:commandButton id="btnSendCreditReminder" value="Send Credit Reminder" 
                                         onclick="confirmation.show()" type="button"
                                         title="Send a reminder to the customer to remind him of outstanding invoice."/>
                    <p:confirmDialog  width="400" message="" header="Sending a credit reminder" severity="alert" widgetVar="confirmation">
                        <f:facet name="message">
                            <h:outputText value="You are going to send a credit reminder to the customer."/><br/>
                            <h:outputText value="Are you sure you want to continue?"/>
                        </f:facet>    
                        <p:commandButton value="Yes" update="invoicesPanel" onclick="confirmation.hide()"
                                         actionListener="#{creditManagerBean.sendCreditReminder}"/>
                        <p:commandButton value="No" update="invoicesPanel" onclick="confirmation.hide()" type="button" />
                    </p:confirmDialog>
                </h:form>
                <p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>  
                <p:dialog modal="true" widgetVar="statusDialog" header="Please wait..."   
                          draggable="false" closable="false" resizable="false">  
                    <p:graphicImage value="../resources/images/ajaxloadingbar.gif" />  
                </p:dialog>
            </ui:define>
        </ui:composition>
    </body>
</html>
